package cn.game.tanke.main;

import cn.game.tanke.panel.TankPanel;
import cn.game.tanke.role.Player;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TankGameFrame extends JFrame {
    private static final Logger LOGGER = Logger.getLogger(TankGameFrame.class.getName());
    private static Image tankicon;

    static {
        try {
            tankicon = ImageIO.read(TankGameFrame.class.getClassLoader().getResourceAsStream("images/boss1.gif"));
        } catch (IOException e) {
            LOGGER.log(Level.SEVERE, "Failed to load tank icon", e);
        }
    }

    public TankGameFrame() {
        setTitle("Tank War");
        setSize(30*32,24*32);
        setLocationRelativeTo(null);
        setIconImage(tankicon);

        TankPanel tankPanel = new TankPanel();
        Player player = Player.getInstance();

        // 加入监听器
        tankPanel.addKeyListener(player);
        tankPanel.setFocusable(true); // 设置面板可获得焦点
        tankPanel.requestFocusInWindow(); // 请求面板获得焦点

        add(tankPanel);
        setResizable(false);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            TankGameFrame tankGameFrame = new TankGameFrame();
        });
    }
}